package com.fzy.modules.goods.web;

import com.fzy.core.common.ApiController;
import com.fzy.core.common.ResponseData;
import com.fzy.core.common.UploadData;
import com.fzy.modules.goods.entity.GoodsPic;
import com.fzy.modules.goods.service.GoodsPicService;
import com.fzy.modules.goods.service.PicService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品图片Controller
 */
@RestController
@RequestMapping(value = "/api/goods/pic")
public class GoodsPicController extends ApiController{
    @Autowired
    GoodsPicService goodsPicService;
    @Autowired
    PicService picService;

    @GetMapping(value = "")
    public ResponseEntity<ResponseData> list(String goodsId){
        return  new ResponseEntity<>(success(goodsPicService.findAllLIst(goodsId)), HttpStatus.OK);
    }
    @PostMapping(value = "")
    public ResponseEntity<ResponseData> save(GoodsPic goodsPic){
        if(StringUtils.isNotEmpty(goodsPic.getId())){
            return  new ResponseEntity<>(success(goodsPicService.update(goodsPic)), HttpStatus.OK);
        }
        return new ResponseEntity<>(success(goodsPicService.save(goodsPic)),HttpStatus.OK);
    }
    @DeleteMapping(value = "/{id}")
    public  ResponseEntity<ResponseData> delete(@PathVariable  String id){
        return new ResponseEntity<>(success(goodsPicService.delete(id)),HttpStatus.OK);
    }
    /**
     * 上传图片
     * @param upfile
     * @return
     */
    @PostMapping("/uploadImage")
    public ResponseEntity<ResponseData> upImageFile(MultipartFile file){
        return  new ResponseEntity<>(success( picService.uploadImage(file)),HttpStatus.OK);
    }
}
